<?php
/* @var $this Model2servController */
/* @var $model Model2serv */
/* @var $form TbActiveForm */
?>

<div class="form">

	<?php
	$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
		'id' => 'model2serv-form',
		// Please note: When you enable ajax validation, make sure the corresponding
		// controller action is handling ajax validation correctly.
		// There is a call to performAjaxValidation() commented in generated controller code.
		// See class documentation of CActiveForm for details on this.
		'enableClientValidation' => true,
		'clientOptions' => array(
			'validateOnSubmit' => true,
		),
	));
	?>


	<?php echo $form->errorSummary($model); ?>

	<?php
	$c = new CDbCriteria();
	$c->select = '"pageId", "pageTitle"';
	$c->addCondition("\"pageType\" = 'category'");
	echo $form->dropDownListControlGroup($model, 'category', CHtml::listData(Page::model()->findAll($c), 'pageId', 'pageTitle'), array('empty' => '--',
		'ajax' => array(
			'type' => 'POST',
			'url' => CController::createUrl('model2serv/getData'),
			'data' => array(
				'id' => 'js:this.value',
				'to' => 'brand'
			),
			'update' => '#brand',
		))
	);
	?>
	<br /><br />

	<?php
	echo $form->dropDownListControlGroup($model, 'brand', $model->getBrands(), array('empty' => '--', 'id' => 'brand',
		'ajax' => array(
			'type' => 'POST',
			'url' => CController::createUrl('model2serv/getData'),
			'data' => array(
				'id' => 'js:this.value',
				'to' => 'model'
			),
			'update' => '#source',
		))
	);
	?>
	<br /><br />
	<?php
	echo $form->dropDownListControlGroup($model, 'modelId', $model->getModels(),
			//array(), 
			array('empty' => 'нет', 'encode' => false, 'id' => 'source', 'style' => 'width:40%'));
	?>    
	<div class="grid-body no-border">
		<div class="row-fluid">
			<table class="table table-bordered no-more-tables table-hover">
				<thead>
					<tr class="">
						<th width="1%">
				<div class="checkbox check-default">
					<input type="checkbox" class="checkall" value="1" id="checkbox20">
					<label for="checkbox20"></label>
				</div>
				</th>
				<th width="12%" class="text-center">Услуга</th>
				<!--<th width="6%" class="text-center">Наличие</th>-->
				<th width="15%" class="text-center">Цена и Наличие</th>
				<th width="6%" class="text-center">Блок</th>
				<th width="6%" class="text-center">Сортировка</th>
				<!--<th width="30%" class="text-center">Запчасти</th>-->
				<th width="1%" class="text-center">Акт.</th>				
				</tr>
				</thead>
				<tbody id="model2serv">

				</tbody>
			</table>
		</div>
	</div>
	<script>
		$(document).ready(function() {
				$.post('<?php echo Yii::app()->createUrl('/admin/model2serv/getService'); ?>', {id: $('#source').val(), action: 'update', catID: $('#Model2serv_category').val()}, function(response) {
					$('#model2serv').html(response);
				});
			$('body').on('click', '.changeAll', function() {
				var val = $(this).prev('input').val();
				$(this).parent('div').parent('td').find('input').val(val);
				$(this).parent('div').parent('td').parent('tr').children('td').find('input[type=hidden]').prop('disabled', 0);
			});
			$('#source').change(function() {
				$.post('<?php echo Yii::app()->createUrl('/admin/model2serv/getService'); ?>', {id: $(this).val(), action: 'update', catID: $('#Model2serv_category').val()}, function(response) {
					$('#model2serv').html(response);
				});
			});
			$('#model2serv').on('keyup', 'tr td div input', function() {
				$(this).parent('div').parent('td').parent('tr').children('td').find('input[type=hidden]').prop('disabled', 0);
			});
			$('#model2serv').on('keyup', '.sort', function() {
				$(this).parent('td').parent('tr').children('td').find('input[type=hidden]').prop('disabled', 0);
			});
			$('#model2serv').on('change', 'tr td div select', function() {
				$(this).parent('div').parent('td').parent('tr').children('td').find('input[type=hidden]').prop('disabled', 0);
			});
                        $('#model2serv').on('change', '.active', function() {
				$(this).parent('span').parent('td').parent('tr').children('td').find('input[type=hidden]').prop('disabled', 0);
			});
			$("#model2serv-form").on('change', '.checkbox :checkbox', function() {
				
				var checked = !$(this).is(':checked') ? true : false;
				if (!checked)
					$(this).parent('div').parent('td').parent('tr').addClass('checked');
				else
					$(this).parent('div').parent('td').parent('tr').removeClass('checked');
				$(this).parent('div').parent('td').parent('tr').find(':input[type=text], select, input[class="active"], input[type="hidden"]').prop('disabled', checked);
				
				var sel = $(this).parent('div').children('input[type="hidden"]').val();
				if (checked && sel > 0) {
					$('#model2serv tr:first td:first div').append('<input type="hidden" name="delete[]" value=' + sel + ' id="d-' + sel + '">');
				} else {
					$('#d-' + sel + '').remove();
				}
			});
		});
	</script>
	<div class="form-actions" style="position: fixed;right: 50px;bottom: 50px;z-index: 10000;border:3px solid #0AA699;">
		 <?php
		 echo TbHtml::submitButton($model->isNewRecord ? 'Create' : 'сохранить', array(
			 'color' => TbHtml::BUTTON_COLOR_PRIMARY,
			 'size' => TbHtml::BUTTON_SIZE_LARGE,
		 ));
		 ?>
    </div>

	<?php $this->endWidget(); ?>

</div><!-- form -->
<style>
	#model2serv tr {
		background-color: #F2CBCB;
	}
	#model2serv tr.checked {
		background-color: #B9EAC3;
	}
</style>